package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import battleFieldModel.BattleField;
import battleFieldModel.Player;

/**
 * The screen that shows up when JavaBoy starts. Should have an option for
 * exiting, starting a new game, and a little eye candy.
 * 
 * @author Cody Mingus
 * 
 */
@SuppressWarnings("serial")
public class TitleView extends MasterViewPanel {

	private class ExitListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			m.saveWvNavigator();
			System.exit(0);//
		}

	}

	private class NewGameListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// m.changeView(Views.BATTLE, new BattleField(new Player()));
			m.changeView(Views.WORLD, new Integer(0)); // change added, as per
														// specs
		}

	}

	private class ContinueGameListener implements ActionListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
		 * )
		 * 
		 * @author Chris Ray Created on 11:30:22 PM Nov 7, 2011
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			m.changeView(Views.WORLD, new Integer(1));

			// only difference between this and the NewGame button, is that this
			// will load a navigator.dat file instead of creating a new one
		}

	}

	private JButton newGame;
	private JButton cont;

	private JButton exit;

	private JButton trade;

	/**
	 * Creates a new TitleView ready to be viewed.
	 * 
	 * @param m
	 */
	public TitleView(MasterView m) {
		super(m);
		setUpLayout();
		setUpButtons();

	}

	/*
	 * Add the JButtons for exiting and starting a new game.
	 */
	private void setUpButtons() {
		newGame = new JButton("New Game");
		newGame.addActionListener(new NewGameListener());
		this.add(newGame);

		cont = new JButton("Continue");
		cont.addActionListener(new ContinueGameListener());
		this.add(cont);

		// makes sense to have exit as the last button.
		trade = new JButton("Trade");
		trade.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				m.changeView(Views.TRADE, null);

			}
		});
		this.add(trade);
		exit = new JButton("Exit");
		exit.addActionListener(new ExitListener());
		this.add(exit);

		// check if navigator.dat file exists, if it does not...disable continue
		// going as per video at 00:02
		File file = new File("navigator.dat");
		if (!file.exists())
			// disable continue
			cont.setEnabled(false);
	}

	/*
	 * Set up a grid layout and add our eye candy
	 */
	private void setUpLayout() {
		this.setLayout(new GridLayout(5, 1)); // change 10/30/11 6:00 am from
												// this.setLayout(new
												// GridLayout(3, 1)); to
												// this.setLayout(new
												// GridLayout(4, 1));
		add(new JLabel(new ImageIcon("images/javaBoy.png")));
	}

	@Override
	public String toString() {
		return "TITLE";
	}

}
